//
//  UnarmedTodayVC.swift
//  SBSafetyApp
//
//  Created by Lina on 2022/12/30.
//
// 【首页-今日未布防】今日未布防页面

import Foundation
import UIKit

// 1 营业网点 2 ATM 3 离行自助 4 办公区域 5 金库
public enum UnarmedTodayType: String {
    case WorkNet = "1"
    case ATMs = "2"
    case SelfHelp = "3"
    case OfficeArea = "4"
    case Treasury = "5"
}

class UnarmedTodayModal: NSObject {
    var type: UnarmedTodayType = .WorkNet
    var name: String?
    var num: Int?
    var total: Int?
    
    init(type: UnarmedTodayType, name: String? = nil, num: Int? = nil, total: Int? = nil) {
        self.type = type
        self.name = name
        self.num = num
        self.total = total
    }
}

class UnarmedTodayVC: SubLevelViewController {
    let tableView = UITableView(frame: .zero, style: .plain)
    
    var datas: [AlarmCbfqyCountModal?] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        navigationController?.navBarStyle(.white)
        title = "今日未布防"
        setupUI()
        reloadData()
    }
    
    func reloadData() {
        view.showToastActivity()
        let param = AlarmCommonParam(dateType: "1", tbCompanyId: "\(deptId ?? -1)")
        // 区域类型统计-撤布防
        API.getAlarmCbfqyCount(withParam: param) { responseModel in
            DispatchQueue.main.async {
                self.datas = responseModel.models ?? []
                self.tableView.reloadData()
                self.view.hideToastActivity()
            }
        }
    }
    
//    @objc func searchTapped() {
//        let searchVC = UnarmedSearchVC()
//        searchVC.didSelectItemWith = { (item) -> () in
////            self.deptName = dept.deptName
////            self.deptId = dept.deptId
//        }
//        navigationController?.pushViewController(searchVC, animated: true)
//    }
    
    // MARK: - Setup
    
    func setupUI() {
        tableView.separatorStyle = .none
        tableView.showsVerticalScrollIndicator = false
        tableView.backgroundColor = .white
        tableView.delegate = self
        tableView.dataSource = self
        view.addSubview(tableView)
        tableView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(view.safeAreaLayoutGuide.snp.top)
            make.bottom.equalTo(view.safeAreaLayoutGuide.snp.bottom)
        }
    }
    
//    func setupNavItems() {
//        let searchBtn = UIButton(type: .custom)
//        searchBtn.frame = CGRect(x: 0, y: 0, width: 60, height: 60)
//        searchBtn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 50, bottom: 0, right: 0)
//        searchBtn.setImage(UIImage(named: "ic_search"), for: .normal)
//        searchBtn.addTarget(self, action: #selector(searchTapped), for: .touchUpInside)
//        let searchBar = UIBarButtonItem(customView: searchBtn)
//    
//        navigationItem.rightBarButtonItems = [searchBar]
//    }
}

extension UnarmedTodayVC: UITableViewDataSource, UITableViewDelegate {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let data = datas[indexPath.row]
        let ID : String = "UnarmedTodayCell"
            
        var cell = tableView.dequeueReusableCell(withIdentifier: ID)
        if cell == nil {
            cell = UnarmedTodayCell(style: .default, reuseIdentifier: ID)
        }
        
        (cell as? UnarmedTodayCell)?.reload(withModal: data)
        return cell!
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let m = datas[indexPath.row]
        let listV = UnarmedListVC(withDeptId: self.deptId, deptName: self.deptName, date: date, label: m?.dictLabel, val: m?.dictValue)
        self.navigationController?.pushViewController(listV, animated: true)
    }
}
